//
//  PictureViewController.m
//  XIWenLeJian
//
//  Created by lanou3g on 15/11/17.
//  Copyright © 2015年 蓝足. All rights reserved.
//

#import "PictureViewController.h"
#import "PictureModel.h"
#import "PictureTableViewCell.h"
#import "NetWorkHandle.h"

@interface PictureViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *pictureTableView;

/**数据数组*/
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation PictureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor yellowColor];
    
    self.pictureTableView.allowsSelection = NO;
    
    [self loadTableView];
}

#pragma mark --- 创建pictureTableView
- (void)loadTableView
{
    self.pictureTableView = [[UITableView alloc] initWithFrame:self.view.frame style:(UITableViewStylePlain)];
    self.pictureTableView.dataSource = self;
    self.pictureTableView.delegate = self;
    [self.pictureTableView registerClass:[PictureTableViewCell class] forCellReuseIdentifier:@"CELL"];
    [self.view addSubview:self.pictureTableView];
}

#pragma mark --- 懒加载加载数据
- (NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        
        _dataArray = [NSMutableArray array];
        
        NSString *url = @"http://api.budejie.com/api/api_open.php?a=list&appname=baisishequ&asid=79C90406-DB8A-4758-9466-DEDB502C2A14&c=data&client=iphone&device=ios%20%E8%AE%BE%E5%A4%87&from=ios&jbk=0&mac=&market=&openudid=3739a3941c7bb4f82c78c8c53228edcb4a14f0d0&page=0&per=20&sub_flag=1&type=10&udid=&ver=3.6";
        
        [NetWorkHandle getDataWithURLString:url compare:^(id object) {
            
            NSDictionary *dict = object;
            
            NSArray *arr = dict[@"list"];
            
            for (NSDictionary *dic in arr) {
                
                PictureModel *model = [PictureModel pictureWithDict:dic];
                
                [_dataArray addObject:model];
            }
            
            NSLog(@"%ld",self.dataArray.count);
            
            [self.pictureTableView reloadData];
        }];
    }
    return _dataArray;
}

#pragma mark --- TableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PictureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
    
    PictureModel *model = self.dataArray[indexPath.row];
    
    cell.pictureModel = model;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 220;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
